#include "MY_Process.h"
#include "MYusart.h"
#include "draw_api.h"
#include "MCU_Flash.h"
#include "Hardware.h"
#include "TimTask.h"
#include "nrf24L01.h"

uint8_t FlAG_ADC = LEISURE, Flag_NRF_RX = LEISURE;
uint8_t Device_Num;
uint16_t ADC_Value = 0, ADCKey_Num = 0;
uint32_t Test_addr = ((uint32_t)0x08020000);
uint8_t NRF_Buf[33] = {0};
uint8_t Device_Adr[11] = {0x01, 0x05, 0x09, 0x0d, 0x0f, 0x10, 0x50, 0x90, 0xd0, 0xf0};

void Task_OLED_Upadata(void)
{
    static uint8_t Cnt = 50;
    OLED_ClearScreen();
    if (Flag_NRF_RX == COMPELETE)
        Cnt = 10;
    if (Cnt > 1)
        Cnt--;
    if (Cnt <= 1)
    {
        OLED_Printf(PRINT_LATER, 0, 0, "None  Device....");
    }
    else if(Cnt >= 1)
    {
        OLED_Printf(PRINT_LATER, 0, 0, "%.2fV > %d%%", (NRF_Buf[1] * 256 + NRF_Buf[2]) * 2.85 / 2048, ((NRF_Buf[1] * 256 + NRF_Buf[2]) * 2850 / 2048 - 3500) / 7);
    }
    OLED_RoundRect(8, 17, 48, 28, 10);
    OLED_RoundRect(72, 17, 48, 28, 10);
    if (NRF_Buf[0] == 0xf0)
        OLED_FillRoundRect(12, 21, 40, 20, 6);
    if (NRF_Buf[0] == 0x0f)
        OLED_FillRoundRect(76, 21, 40, 20, 6);
    Flag_NRF_RX = LEISURE;
    OLED_Printf(PRINT_NOW, 6, 0, "Target:%d Enter", Device_Num);
}

void Task_UART_Print(void)
{
    Uart_Printf("Key: 0x%2x, Bat: %.4fV, >>> %d%%, Taget: %d\n\n", NRF_Buf[0], (NRF_Buf[1] * 256 + NRF_Buf[2]) * 3.3 / 2048, NRF_Buf[3], NRF_Buf[4]);
}

void Task_LED(void)
{
    HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
}

void Task_ADC(void)
{
    if (FlAG_ADC == LEISURE)
    {
        HAL_ADC_Start(&hadc1);
        HAL_ADC_Start_IT(&hadc1);
        FlAG_ADC = WORKING;
    }
}

void Task_ADCKey_Scan(void)
{
    if (FlAG_ADC == COMPELETE)
    {
        ADCKey_Num = Key_Scan(ADC_Value);
        FlAG_ADC = LEISURE;
        switch (ADCKey_Num)
        {
        case 0:
            break;
        case 1:
            Device_Num--;
            break;
        case 2:
            HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
            MCU_Flash_Erase(5, 6);
            MCU_Flash_Writedata(&Device_Num, 1, Test_addr);
            break;
        case 3:
            Device_Num++;
            break;

        default:
            break;
        }
    }
}

void Task_NRF(void)
{
    if (NRF_MODE_NOW == NRF_MODE_TX)
    {
        NRF24L01_TX_Mode(Device_Adr + Device_Num);
        switch (NRF24L01_TxPacket(NRF_Buf))
        {
        case TX_OK:
            OLED_Printf(PRINT_LATER, 4, 0, "TX Success");
            break;
        case MAX_TX:
            OLED_Printf(PRINT_LATER, 4, 0, "TX MAX    ");
            break;
        case 0xFF:
            OLED_Printf(PRINT_LATER, 4, 0, "TX other  ");
            break;
        default:
            break;
        }
        // Uart_Printf("IRQ: %d    Sta: %d\n", HAL_GPIO_ReadPin(NRF_IRQ_GPIO_Port, NRF_IRQ_Pin), NRF24L01_GetSta());
    }
    else if (NRF_MODE_NOW == NRF_MODE_RX)
    {
        NRF24L01_RX_Mode(Device_Adr + Device_Num);
        if (NRF24L01_RxPacket(NRF_Buf) == 0)
            Flag_NRF_RX = COMPELETE;
    }
}
